using System;
using System.Linq;
using Canlipe.ServiceInterfaces;

namespace Canlipe
{
	public class Config
	{

		public Config ()
		{
			this.SiteTitle = string.Empty;
			this.SourceFolder = string.Empty;
			this.OutputFolder = string.Empty;
			this.BaseUrl = string.Empty;
		}

		public string SiteTitle { get; set; }

		public string SourceFolder { get ; set; }

		public string OutputFolder { get; set; }

		public string BaseUrl { get; set; }

		public static Config FromString (string str, ILogger logger)
		{
			var cfg = new Config ();
			var settingStrings = str.Split (new [] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
			foreach (var settingString in settingStrings) {
				var pcs = settingString
					.Split (new [] { ':' }, 2, StringSplitOptions.None)
					.Select (o => o.Trim ()).ToArray ();
				if (pcs.Length != 2) { 
					logger.Log (
						string.Format ("Error parsing setting \"{0}\"", settingString));
					continue;
				}

				switch (pcs [0]) {
				case "SourceFolder":
					cfg.SourceFolder = pcs [1];
					break;
				case "OutputFolder":
					cfg.OutputFolder = pcs [1];
					break;
				case "BaseUrl":
					cfg.BaseUrl = pcs [1];
					break;
				case "SiteTitle":
					cfg.SiteTitle = pcs [1];
					break;
				default:
					logger.Log (
						string.Format ("Unknown setting key \"{0}\"", pcs [0]));
					break;
				}
			}
			return cfg;
		}
	}
}

